package org.makcro.sms.services.chat.web.client.widgets.bulk;

import org.makcro.sms.services.chat.web.client.AppEvents.BulkEvents;
import org.makcro.sms.services.chat.web.client.Constants;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;

public class SendBulkMessageDialog extends Dialog {
	private TextField<String>	receiverTextField;
	private TextArea			txtrNewTextarea;
	private Button				btnSend;
	private Button				btnCancel;

	public SendBulkMessageDialog() {
		setModal( true );
		setHeading( "Slanje bulk poruke" );
		setBodyStyle( "padding: 8px;background: none" );
		setButtons( "" );
		setWidth( 600 );
		setHeight( 400 );
		setLayout( new FormLayout() );
		FormData fd_nmbrfldReceiver = new FormData( "95%" );
		fd_nmbrfldReceiver.setMargins( new Margins( 10, 0, 0, 0 ) );
		add( getReceiverTextField(), fd_nmbrfldReceiver );
		add( getTxtrNewTextarea(), new FormData( "95% -90" ) );
		getButtonBar().add( getBtnSend() );
		getButtonBar().add( getBtnCancel() );
	}

	public TextField<String> getReceiverTextField() {
		if (receiverTextField == null) {
			receiverTextField = new TextField<String>();
			receiverTextField.setAllowBlank( false );
			receiverTextField.setFieldLabel( "Primalac" );
		}
		return receiverTextField;
	}

	public TextArea getTxtrNewTextarea() {
		if (txtrNewTextarea == null) {
			txtrNewTextarea = new TextArea();
			txtrNewTextarea.setFieldLabel( "Text poruke" );
		}
		return txtrNewTextarea;
	}

	private Button getBtnSend() {
		if (btnSend == null) {
			btnSend = new Button( "Posalji" );
			btnSend.addListener( Events.Select, new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {
					onBtnSendSelected( ce );
				}
			} );
		}
		return btnSend;
	}

	protected void onBtnSendSelected(ButtonEvent ce) {
		AppEvent appEvent = new AppEvent( BulkEvents.BULK_MESSAGE_ABOUT_TO_BE_SENT );
		
		appEvent.setData( Constants.EventData.BULK_RECEIVER, getReceiverTextField().getValue());
		appEvent.setData( Constants.EventData.BULK_MESSAGE_TEXT, getTxtrNewTextarea().getValue());
		getButtonBar().disable();
		
		Dispatcher.get().dispatch( appEvent );
	}

	private Button getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new Button( "Otkazi" );
			btnCancel.addListener( Events.Select, new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {
					onBtnCancelSelected();

				}
			} );
		}
		return btnCancel;
	}

	protected void onBtnCancelSelected() {
		this.hide();

	}
}
